package com.drawu.drawuspring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drawu.drawuspring.domain.Result;
import com.drawu.drawuspring.domain.VO.MessageVo;
import com.drawu.drawuspring.domain.VO.PostVO;
import com.drawu.drawuspring.domain.VO.UserVO;
import com.drawu.drawuspring.domain.po.ActComments;
import com.drawu.drawuspring.domain.po.Message;
import com.drawu.drawuspring.domain.po.Users;
import com.drawu.drawuspring.enums.AppHttpCodeEnum;
import com.drawu.drawuspring.mapper.ActCommentsMapper;
import com.drawu.drawuspring.mapper.MessageMapper;
import com.drawu.drawuspring.service.*;
import com.drawu.drawuspring.utils.AppThreadLocalUtil;
import com.drawu.drawuspring.utils.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 消息表[5](@ref) 服务实现类
 * </p>
 *
 * @author hui
 * @since 2025-04-01
 */
@Service
@RequiredArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    public final IForumService forumService;
    public final IUsersService usersService;

    @Override
    public void saveMessage(Message message) {
        this.save(message);
    }

    @Override
    public void deleteMessage(Message message) {
        QueryWrapper<Message> queryWrapper = Wrappers.query();
        queryWrapper.eq("user_id", message.getUserId());
        queryWrapper.eq("target_id", message.getTargetId());
        queryWrapper.eq("source_id", message.getSourceId());
        queryWrapper.eq("type", message.getType());
        this.remove(queryWrapper);
    }

    @Override
    public Result getMessages() {
        // 获取当前用户id
        Users user = AppThreadLocalUtil.getUser();
        if (user == null || user.getId() == null) {
            return Result.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = user.getId();
        QueryWrapper<Message> queryWrapper = Wrappers.query();
        queryWrapper.eq("target_id", userId).orderByDesc("create_time");
        List<Message> list = this.list(queryWrapper);
        List<Integer> userIds = list.stream().map(Message::getUserId).distinct().toList();
        List<Users> userList = usersService.listByIds(userIds);
        Map<Integer, UserVO> userMap = userList.stream().collect(Collectors.toMap(Users::getId, u -> BeanUtils.copyBean(u, UserVO.class)));
        List<MessageVo> messageVos = BeanUtils.copyList(list, MessageVo.class);
        for (MessageVo messageVo : messageVos) {
             messageVo.setUserVo(userMap.get(messageVo.getUserId()));
        }
        return Result.success(messageVos);
    }
}
